function playAudio() {
    var audio = document.getElementById("myAudio");
    var playButton = document.getElementById("play_buttom");
    var lyric_img = document.getElementById("lyric_img");
    var shoubi = document.getElementById("shoubi");

    if (audio.paused) {
        lyric_img.style.animation = "infiniteRotate 48s linear infinite";
        shoubi.style.transform = 'rotate(-15deg)'
        audio.play();
        // 更改为播放状态的背景图片
        playButton.style.backgroundImage = "url(/source/playing.png)";
    } else {
        lyric_img.style.animationPlayState = "paused";
        shoubi.style.transform = 'rotate(-40deg)';
        audio.pause();
        // 更改为暂停状态的背景图片
        playButton.style.backgroundImage = "url(/source/stop.png)";
    }
}

function playByIndex(index) {
    if (index < 0 || index >= music_hash.length) return;
    currentIndex = index;
    // 动态加载歌曲信息
    const hash_script = document.createElement('script');
    hash_script.src = `http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&format=jsonp&hash=${encodeURIComponent(music_hash[index])}&callback=handleHashResponse`;
    document.body.appendChild(hash_script);
    hash_script.onload = () => document.body.removeChild(hash_script);
}

function rightAudio() {
    if (music_hash.length == 0) return;
    let nextIndex = currentIndex + 1;
    if (nextIndex >= music_hash.length) nextIndex = 0; // 循环到第一首
    playByIndex(nextIndex);
}

function leftAudio() {
    if (music_hash.length === 0) return;
    let prevIndex = currentIndex - 1;
    if (prevIndex < 0) prevIndex = music_hash.length - 1; // 循环到最后一首
    playByIndex(prevIndex);
}